package com.dragon.client.config.aop;


import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.dragon.client.config.annotation.SendReminder;
import com.dragon.logical.service.auth.IDictsortDetailService;
import com.dragon.logical.service.reminder.IDoReminderService;
import com.dragon.repository.entity.DoReminder;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Map;


@Aspect
@Component
public class SendReminderAop {

    @Autowired
    IDoReminderService reminderService;

    @Autowired
    IDictsortDetailService detailService;

    @Pointcut("@annotation(com.dragon.client.config.annotation.SendReminder)")
    public void sendRemindPointCut(){

    }

    @AfterReturning(pointcut = "sendRemindPointCut()",returning = "returnValue")
    public void  doAfterReturning(JoinPoint joinPoint,Object returnValue){
        Map<String,Object> result = JSONObject.parseObject(JSONObject.toJSONString(returnValue),Map.class);
        Map<String,Object> params = JSONObject.parseObject(JSONObject.toJSONString(result.get("data")),Map.class);
        handleOperationRecord(joinPoint,params);
    }

    private void handleOperationRecord(JoinPoint joinPoint,Map<String,Object> returnValue){
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        assert sra != null;
        HttpServletRequest request = sra.getRequest();
        //接收消息id
        Integer toId = (Integer) returnValue.get("toId");
        //发送消息id
        Integer fromId = (Integer) returnValue.get("fromId");
        //关系id
        Integer relationId = (Integer) returnValue.get("relationId");

        SendReminder sendReminder = getAnnotation(joinPoint);
        //获取对应操作流程类型名称
        DoReminder doReminder = new DoReminder();
        doReminder.setAdminId(toId);
        doReminder.setFromId(fromId);
        doReminder.setType(sendReminder.noticeType());
        doReminder.setCorrlelationId(relationId);
        doReminder.setCreateTime(DateUtil.date());
        doReminder.setUrlAddress(sendReminder.pushUrl());
        doReminder.setOperationType(sendReminder.operationType());
        //reminderService.addDoReminder(doReminder);
    }

    /**
     * 是否存在注解，如果存在就获取，不存在则返回null
     * @param joinPoint
     * @return
     */
    private SendReminder getAnnotation(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        if (method != null) {
            return method.getAnnotation(SendReminder.class);
        }
        return null;
    }

}
